import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

public class LinkedList<T>{
    private GoodsLinkNode head = new GoodsLinkNode(0, "", 0, "", "", "");


    public void add(GoodsLinkNode goodsNode) {
        GoodsLinkNode temp = head;
        while (temp.next != null) {
            temp = temp.next;
        }
        temp.next = goodsNode;
    }
    public void addByNumber(GoodsLinkNode goodsNode) {
        GoodsLinkNode temp = head;
        boolean flag = false;
        while (true) {
            if (temp.next == null)
            {
                break;
            }
            if (temp.next.number > goodsNode.number)
            {
                break;
            } else if (temp.next.number == goodsNode.number)
            {
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (flag) {
            System.out.println("准备添加商品的编号:" + goodsNode.number + "已经存在不能添加了");
        } else {
            goodsNode.next = temp.next;
            temp.next = goodsNode;
        }
    }
    public void showlist() {
        if (head.next == null) {
            System.out.println("链表为空");
            return;
        }
        GoodsLinkNode temp = head.next;
        while (temp != null) {
            System.out.print(temp);//前面GoodsLinkNode类中已经重写toString方法
            System.out.println();
            temp = temp.next;
        }
    }
    public void backlist() {

        if (head.next == null) {
            System.out.println("链表为空");
            return;
        }
        GoodsLinkNode temp = head.next;
        int amount = 0;
        while (temp != null) {
            if (Objects.equals(temp.dead, temp.date)) {
                amount++;
                System.out.println("名称：" + temp.name+ "出库预警");
            }
            temp = temp.next;
        }
        System.out.println("共" + amount + "个");
    }
    public int len() {
        int count = 0;
        if (head.next == null) {
            System.out.println("链表为空");
            return 0;
        }
        GoodsLinkNode temp = head.next;
        while (temp != null) {

            count++;

            temp = temp.next;
        }
        return count;

    }


    public void delete(int number) {
        GoodsLinkNode temp = head;
        boolean flag = false;
        while (true) {
            if (temp.next == null) {
                break;
            }
            if (temp.next.number == number) {

                flag = true;
                break;
            }
            temp = temp.next;
        }

        if (flag) {

            temp.next = temp.next.next;
        } else {
            System.out.println("编号" + number + "的商品不存在");
        }
    }

    public void update(GoodsLinkNode goodsNode) {

        if (head.next == null) {
            System.out.println("链表为空");
            return;
        }
        GoodsLinkNode temp = head.next;
        boolean flag = false;
        while (true) {
            if (temp == null) {
                break;
            }
            if (temp.number == goodsNode.number)
            {
                flag = true;
                break;
            }
            temp = temp.next;
        }

        if (flag) {
            temp.name = goodsNode.name;
            temp.price = goodsNode.price;
        } else {
            System.out.println("没有找到编号为" + goodsNode.number + "的商品");
        }
    }

    public void getbyDescribe(String describe) {
        GoodsLinkNode temp = head;
        int count = 0;
        System.out.println("分类为" + describe + "的商品有：");
        while (temp != null) {
            if (Objects.equals(temp.describe, describe)) {
                count++;
                System.out.println("编号：" + temp.number +
                        "名称：" + temp.name +
                        "生产日期：" +temp.date +
                        "过期日期：" + temp.dead);
            }
            temp = temp.next;
        }
        System.out.println("共上面" + count + "个");
    }
      public void getbyDate(String date) {
          GoodsLinkNode temp = head;
          int count = 0;
          System.out.println("分类为" + date + "的商品有：");
          while (temp != null) {
              if (Objects.equals(temp.describe, date)) {
                  count++;
                  System.out.println("编号：" + temp.number +
                          "名称：" + temp.name +
                          "生产日期：" +temp.date +
                          "过期日期：" + temp.dead);
              }
              temp = temp.next;
        }
          System.out.println("生产日期为" + date + "的商品有：");
    }
}